//
//  YCTabBarButton.m
//  lianganyuchu
//
//  Created by iMac on 17/2/20.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCTabBarButton.h"

static NSMutableArray      *_normalArray;
static NSMutableArray      *_selectdArray;
#define labHeight 3
@interface YCTabBarButton ()
{

}
@end
@implementation YCTabBarButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //初始化
    }
    return self;
}

+(id)buttonWithFrame:(CGRect)frame withIndex:(int)index withTitle:(NSString *)title
{
    if (!_normalArray) {
        _normalArray = [NSMutableArray arrayWithCapacity:1];
    }
    if (!_selectdArray) {
        _selectdArray = [NSMutableArray arrayWithCapacity:1];
    }

    YCTabBarButton *button = [YCTabBarButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.choiceType = YFChoiceButtonStatusTypeUnselected;
    button.index = index;
    
    UILabel *labTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-labHeight)];
    [labTitle setText:title];
    [labTitle setFont:[UIFont LightFontOfSize:15]];
    [labTitle setTextAlignment:NSTextAlignmentCenter];
    [button addSubview:labTitle];
    button.labTitle=labTitle;
    [labTitle setTextColor:[UIColor lightGrayColor]];
    
    CGSize size=frame.size;
    UILabel *labInditor=[[UILabel alloc]initWithFrame:CGRectMake(size.width*0.15, size.height-labHeight, size.width*0.7, labHeight)];
    [labInditor setBackgroundColor:MAIN_COLOR];
    [button addSubview:labInditor];
    [labInditor setHidden:YES];
    button.labIndator=labInditor;
    
    [button addTarget:self
               action:@selector(buttonAction:)
     forControlEvents:UIControlEventTouchUpInside];
    if (button.choiceType == YFChoiceButtonStatusTypeUnselected) {
        [_normalArray addObject:button];
    }else {
        [_selectdArray addObject:button];
    }
    return button;
}

+ (void)buttonAction:(YCTabBarButton *)yfButton {
    if (yfButton.choiceType == YFChoiceButtonStatusTypeSelected) {
        return;
    }else{
        [yfButton.labTitle setTextColor:MAIN_COLOR];
        [yfButton.labIndator setHidden:NO];
        yfButton.choiceType = YFChoiceButtonStatusTypeSelected;
    }
}

- (void)setChoiceType:(YFChoiceButtonStausType)choiceType {
    _choiceType = choiceType;
    
    if (choiceType == YFChoiceButtonStatusTypeSelected) {
        for (int i = 0; i < _selectdArray.count; i++) {
            YCTabBarButton *button = _selectdArray[i];
            button.choiceType = YFChoiceButtonStatusTypeUnselected;            
        }
        [_normalArray removeObject:self];
        [_selectdArray addObject:self];
        [self.labTitle setTextColor:MAIN_COLOR];
        [self.labTitle setFont:[UIFont FontOfSize:18]];
        [self.labIndator setHidden:NO];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(YFChoiceButtonSelectedIndex:)]) {
            [self.delegate YFChoiceButtonSelectedIndex:self.index];
        }
    }else if (_choiceType == YFChoiceButtonStatusTypeUnselected) {        
        [self.labTitle setTextColor:TEXT_LIGHT];
        [self.labTitle setFont:[UIFont LightFontOfSize:15]];
        [self.labIndator setHidden:YES];
        [_normalArray addObject:self];
        [_selectdArray removeObject:self];
    }
}


@end
